package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.common.info.DeviceGroupInfo;
import com.task.core.domain.info.DataContentInfo;
import com.task.core.dto.response.DataContentInfoResposeDTO;
import com.task.core.vo.resp.DatasourceDetailRespVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.DatasourceOptionRequestDTO;
import com.task.core.dto.request.DatasourceRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.DatasourceService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:29
 */
@Api("")
@RestController
@RequestMapping("/datasource")
public class DatasourceController extends BaseController {

  @Resource
  private DatasourceService datasourceService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('dataSource:list')")
  @GetMapping("/getDatasourceByPage")
  public TableDataInfo getDatasourceByPage(DatasourceRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(datasourceService.getDatasourceByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('dataSource:option')")
  @GetMapping("/getDatasourceForOption")
  public TableDataInfo getDatasourceForOption(DatasourceRequestDTO request) {
    return getDataTable(datasourceService.getDatasourceForOption(request));
  }


  @PreAuthorize("@ss.hasPermi('dataSource:option')")
  @GetMapping("/getDatasourceForOptionByMission")
  public TableDataInfo getDatasourceForOptionByMission(DatasourceOptionRequestDTO request) {
    return getDataTable(datasourceService.getDatasourceForOptionByMissionCode(request));
  }

  @Log(title = "数据源管理", businessType = BusinessType.INSERT)
  @PostMapping("/addDatasource")
  public AjaxResult addDatasource(@RequestBody DatasourceRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    datasourceService.addDatasource(request);
    return success();
  }

  @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyDatasource")
  public AjaxResult modifyDatasource(@RequestBody DatasourceRequestDTO request) {
    checkService.check("data_datasource", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    datasourceService.modifyDatasource(request);
    return success();
  }

  @Log(title = "数据源管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteDatasource/{datasourceCode}")
  public AjaxResult deleteDatasource(@PathVariable String datasourceCode) {
    checkService.check("data_datasource", datasourceCode);
    datasourceService.deleteDatasource(datasourceCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

  @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
  @GetMapping ("/clearDataSource")
  public AjaxResult clearDatasource(String dataSourceCode) {
    checkService.check("data_datasource", dataSourceCode);
    datasourceService.clearDatasource(dataSourceCode);
    return success();
  }


  @GetMapping ("/showDataSource")
  public AjaxResult showDatasource(String dataSourceCode) {
    checkService.check("data_datasource", dataSourceCode);
    DataContentInfoResposeDTO dataContentInfo = datasourceService.showDatasource(dataSourceCode);
    return success(dataContentInfo);
  }

  @GetMapping ("/getDataSoucreDetail/{dataSourceCode}")
  @PreAuthorize("@ss.hasPermi('system:datasource:edit')")
  public AjaxResult getDataSoucreDetail(@PathVariable String dataSourceCode) {
    checkService.check("data_datasource", dataSourceCode);
    DatasourceDetailRespVo datasourceDetailRespVo = datasourceService.getDataSoucreDetail(dataSourceCode);
    return success(datasourceDetailRespVo);
  }

}
